<%@ page import="dao.ProductDAO" %>
<%@ page import="entity.Product" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>产品页</title>
    <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>
<div class="container">
    <table class="table table-bordered table-condensed table-hover table-striped">
        <tr>
            <th>商品编号</th>
            <th>商品名称</th>
            <th>商品单价</th>
            <th>商品库存</th>
            <th>操作</th>
        </tr>
        <%
            ProductDAO productDAO = new ProductDAO();
            List<Product> productList = productDAO.queryAllProduct();
            for (Product pro : productList) {
        %>
        <tr>
            <td><%=pro.getId()%></td>
            <td><%=pro.getName()%></td>
            <td><%=pro.getPrice()%></td>
            <td><%=pro.getCount()%></td>
            <td>
                <a href="javascript:sub(<%=pro.getId()%>)" class="btn btn-default">-</a>
                <input type="number" readonly value="1" style="width:70px;" id="num<%=pro.getId()%>">
                <a href="javascript:sup(<%=pro.getId()%>,<%=pro.getCount()%>)" class="btn btn-default">+</a>

                <a href="javascript:buy(<%=pro.getId()%>)" class="btn btn-success"><b
                    class="glyphicon glyphicon-shopping-cart"></b>购买</a>
            </td>
        </tr>
        <%
            }
        %>
    </table>
</div>
<script>
    // 下
    function sub(id) {
        var e_text = document.getElementById("num" + id);
        if ( parseInt(e_text.value) > 1 ) {
            e_text.value = parseInt(e_text.value) - 1;
        }
    }

    // 上
    function sup(id,count) {
        var e_text = document.getElementById("num" + id);
        if ( parseInt(e_text.value) < parseInt(count)) {
            e_text.value = parseInt(e_text.value) + 1;
        }
    }

    function buy(id) {
        var e_text = document.getElementById("num" + id);
        window.location.href=
                "dobuy.jsp?pid=" + id + "&uid=1&count=" + e_text.value;
    }
</script>
</body>
</html>
